#include "icc.h"

xSemaphoreHandle xATRLock ;

static void smart_card_ISR(int event)
{
    if (event) {
	icc_debug("[ISR] insert\r\n");
	xSemaphoreGiveFromISR( xATRLock, NULL );
    } else {
	icc_debug("[ISR] remove\r\n");
    }
}

static int smart_card_init(void)
{
	int	result = COMMON_ERR_UNKNOWN;
	EMV_SLOT_CONFIG params;

	params.clock = MML_SCCLOCK_4_8MHz;
	params.power = N_MML_SC_POWER_5V;
	params.protocol = MML_SCPROTOCOL_T1;
	params.mode = EMV_MODE_T1_IFSD;
	params.timernum = 0;
	result = emv_init(&params);

	if(result){
		icc_debug("[ICC] %s, %d::: error!\n", __func__, __LINE__);
		return result;
	}

	mml_sc_init_presence_card(smart_card_ISR);
	
    return result;
}

void icc_task( void *pvParameters )
{
	int status;
	int result = NO_ERROR;

	smart_card_init();

	xATRLock =  xSemaphoreCreateMutex();
	xSemaphoreTake( xATRLock, 0 );

	while(1){
		while (xSemaphoreTake( xATRLock, 0xFFFF ) != pdTRUE);

		if ( !( status & MML_SCSTATUS_CARD_NOT_INSERTED ) )
		{
			result = emv_test();
			if(NO_ERROR != result){
				
			}else{

			}
		}
		//mml_sc_intr_mask_presence_card();
	}
}
  


